/**
 * Yi-Jing (I Ching) is one of the most ancient literature in Chinese history.  A more detailed
 * description of the system can be found at <a href="{@link}http://en.wikipedia.org/wiki/I_Ching>
 * Wikipedia: I Ching</a>.
 */
package divination.yijing;

import java.util.Random;

/**
 * @author Ting-Shuo Yo
 * A Trigram is a combination of 3 Yao, and hence has totally 8 varieties.
 */
public class Trigram {
	private Yao[] gua = new Yao[3];
	/**
	 * Constructor: initialize 3 Yao with "Yang"
	 */
	public Trigram(){
		for (int i=0;i<3;i++){this.gua[i] = new Yao();}
	}
	
/* Access the private content */
    
    /**
     * Retrieve the Gua, a Yao array of 3.
     * @return Yao[], the Gua.
     */
    public Yao[] getGua(){return(this.gua);}
    
    /**
     * Set the Gua with 3 given Yao
     * @param g Yao[], 3 Yao
     */
    public void setGua(Yao[] g){
    	for(int i=0;i<3;i++){
    		this.gua[i].setYao(g[i].getYao());
    	}
    }

    /**
     * Set the Gua with the given index: 0~7 represents 乾,兌,離,震,巽,坎,艮,坤 
     * @param g Integer, the index of the Gua
     */
    public void setGua(int g){
		int b = 7-g;
		this.gua[0].setYao(((b/4)==1));
		this.gua[1].setYao((((b/2)%2)==1));
		this.gua[2].setYao(((b%2)==1));
    }

/* Renew the Trigram */
	/**
	 * Renew the Trigram with the given random seed
	 * @param rd
	 */
	public void renew(Random rd){
		for (int i=0;i<3;i++){
			gua[i].renew(rd);
		}
	}

	/**
	 * Renew the Trigram without a random seed
	 * @param rd
	 */
	public void renew(){
		for (int i=0;i<3;i++){
			gua[i].renew();
		}
	}
	
/* Trigram look-up */
	private int guaIndex(){
		int idx = 0;
		for(int i=0;i<3;i++){
			idx+=(Math.pow(2,(2-i))*(gua[i].getYao()?1:0));}
		return(7-idx);
	}
	
	public String getName(){
		return(this.trigram_names[this.guaIndex()]);
	}

	public String getFullName(){
		return(this.trigram_fullnames[this.guaIndex()]);
	}

	
/* String visualization of the Trigram */
    public String toString(){
    	String out = "";
    	for(int i=0;i<3;i++){
    		out = this.gua[i].toString() + "\n" + out;
    	}
    	return(out);
    }
    
/* Data */
	public String[] trigram_names = {
			"乾 (quin)","兌 (dui)","離 (li)","震 (zhen)",
			"巽 (xun)","坎 (kan)","艮 (gen)","坤 (kun)"
	};
	
	public String[] trigram_fullnames = {
			"乾 (quin) 天 \t健\t父\t西北",
			"兌 (dui)  澤 \t悦\t少女\t西",
			"離 (li)   火 \t麗\t中女\t南",
			"震 (zhen) 雷 \t動\t長男\t東",
			"巽 (xun)  風 \t入\t長女\t東南",
			"坎 (kan)  水 \t陷\t中男\t北",
			"艮 (gen)  山 \t止\t少男\t東北",
			"坤 (kun)  地\t順\t母\t西南"
	};
	
	public String[] trigram_nature = {"地","山","水","風","雷","火","澤","天"};
}
